﻿using SharedFileDownload.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SharedFileDownloadForm
{
	/// <summary>
	/// Interaction logic for Login.xaml
	/// </summary>
	public partial class Login : Window
	{
		string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.ini");
		public Login()
		{
			InitializeComponent();
			GetUserForDisk();
		}

		private void button_Click(object sender, RoutedEventArgs e)
		{
			var username = textBox.Text;
			var pwd = passwordBox.Password;

			if (FileShared.connectState(txtPath.Text, username, pwd))
			{
				//记录账户路径和密码
				WirteUserToDisk(txtPath.Text, username, pwd);

				MessageBox.Show("连接成功！");
				//跳转mainbox。关闭本窗体
				this.Hide();
				new MainWindow().Show();
			}
			else
			{
				MessageBox.Show("连接失败！");
			}
		}
		private void WirteUserToDisk(string url, string name, string pwd)
		{
			try
			{
				if (File.Exists(path)) { File.Delete(path); }

				using (StreamWriter SW = new StreamWriter(path))
				{
					SW.WriteLine(url+"|"+ name + "|"+ pwd);
				}
			}
			catch (Exception ex)
			{


			}

		}
		private void GetUserForDisk()
		{
			try
			{

				if (File.Exists(path))
				{
					string[] str =null;
					using (StreamReader read = new StreamReader(path))
					{
						str = read.ReadLine().Split('|');
					}
					txtPath.Text=str[0];
					textBox.Text = str[1];

					passwordBox.Password = str[2];
				}
			}
			catch (Exception ex)
			{


			}

		}
	}
}
